package com.group2.bankonline.controller;

import com.group2.bankonline.pojo.Feedback;
import com.group2.bankonline.pojo.Personinfo;
import com.group2.bankonline.service.IFeedbackService;
import com.group2.bankonline.utils.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syf
 * @since 2024-07-15
 */
@RestController
@RequestMapping("/mobilebank/feedback")
public class FeedbackController {

    @Autowired
    private IFeedbackService iFeedbackService;

    //设置客服登录账号及密码
    private String default_name = "feedback";
    private String default_pwd = "123456";

    /*
     * 客服登录（固定账号密码）
     * */
    @RequestMapping("/flogin")
    public JsonResult flogin(@RequestBody LoginFeedback loginFeedback) {
        if (default_name.equals(loginFeedback.getName())&&default_pwd.equals(loginFeedback.getPassword())){
            return new JsonResult(true,"登陆成功");
        }else {
            return new JsonResult(false,"账号或密码错误，请您重新登录");
        }
    }


    /*
    * 账户查看自身反馈
    * */
    @RequestMapping("/getfeedbackbya")
    public ResponseJson getFeedbackFromAccount(@RequestBody RequestFeedback requestFeedback){
        Personinfo personinfo = MyJWT.getPersonByToken(requestFeedback.getToken());
        int id = personinfo.getId();
        int status = requestFeedback.getStatus();
        List <Feedback> feedbacks = iFeedbackService.getFeedbackFromUser(id, status);
        if(feedbacks==null){
            return new ResponseJson("400","未找到相关数据。");
        }else{
            return new ResponseJson("200","查询成功，数据如下。",feedbacks);
        }
    }

    /*
    * 客服查看反馈
    * */
    @RequestMapping("/getanswerfeedback")
    public ResponseJson getFeedbackAnswer(@RequestBody RequestStatus requestStatus){
        int status = requestStatus.getStatus();

        List <Feedback> feedbacks = iFeedbackService.getFeedbackAnswer(status);
        if(feedbacks==null){
            return new ResponseJson("400","未找到相关数据。");
        }else{
            return new ResponseJson("200","查询成功，数据如下。",feedbacks);
        }

    }
    /*
    * 账户添加反馈
    * */
    @RequestMapping("/feedback")
    public ResponseJson saveFeedbackByAccount(@RequestBody RequestContent requestContent){
        Feedback feedback = new Feedback();
        int id = MyJWT.getPersonByToken(requestContent.getToken()).getId();
        feedback.setPersonid(id);
        feedback.setContent(requestContent.getContent());
        feedback.setStatus(0);
        boolean result = iFeedbackService.save(feedback);
        if(result){
            return new ResponseJson("200","您已成功提交反馈！");
        }else{
            return new ResponseJson("400","提交失败，请重新提交！");
        }
    }
    /*
    * 客服反馈
    * */
    @RequestMapping("/answerfeedback")
    public ResponseJson saveAnswerFeedback(@RequestBody Feedback feedback){
        int id = feedback.getId();
        String feed = feedback.getFeedback();
        int status = 1;
        int result = iFeedbackService.updateFeedback(feed,status,id);
        if(result>0){
            return new ResponseJson("200","您已回复该反馈！");
        }else{
            return new ResponseJson("400","回复失败，请重新提交！");
        }
    }
}
